include([
	'renderers.Renderer'
	, 'materials.Material'], 
	
function(){

	declare('renderers.NodeRenderer');
		
	NodeRenderer = Renderer.extend({
		material : Material.standard,
		init : function(name, includeChildren){
			this._super(name || 'NodeRenderer' + (Renderer.count++));
			this.includeChildren = includeChildren;
		},
		render : function(gw){
			if( this.rendering ) return;
	
			this.rendering = true;
			
			var c = gw.c;
			for(var e in gw.entities ){
				var entity = gw.entities[e];
				
				this._render(gw, gw.entities, e);
				
			}
			this.rendering = false;
		},
		_render : function(gw, parent, childKey){
			var c = gw.c;
			var child = parent[childKey];
			if(child && !(typeof(child) == 'function' )){
				c.save();
				var pass = 0;
				try{
					if( child.clz == 'Node' ){
						pass = this.renderNode(gw, child);
					}else{
						pass = this.renderObject( gw, child );	
					}
				}finally{
					if( !pass ){
						console.log("Removing [" + childKey + "] from ");
						console.debug(parent);
						parent[childKey] = null;	
					}
				}
				c.restore();
			}
		},	
		renderNode : function(gw, node){
			var c = gw.c;
			c.save();
			var m2d = node.getWorldTransform();
			var self = this;
			c.setTransform(m2d.x1,m2d.y1, m2d.x2, m2d.y2, m2d.x3,m2d.y3);
			for(var member in node.children){
				this._render(gw, node.children, member);
			}
			node.children.forEach(function(item, index){
				self._render(gw, node.children, index);
			});
			c.restore();
			return true;
		},	
		renderObject : function(gw, obj){
			this.material.render(gw, obj);
			return true;
		}
	});

})